<template>
    <div class="mind-node">
        <div class="node-content">
            <div>
                <span>{{ node.label }}</span>
            </div>
            <div v-if="isEditing" class="edit-content">
                <input type="text" v-model="newLabel" @keyup.enter="saveLabel" />
                <button @click="saveLabel">Save</button>
            </div>
            <div>
                <button class="add-child-btn" @click="addChild">+</button>
                <button class="edit-btn" @click="toggleEdit">Edit</button>
                <button class="delete-btn" @click="deleteNode">Delete</button>
            </div>
        </div>
        <div class="children">
            <mind-node v-for="child in node.children" :key="child.id" :node="child" @add-child="addChild"
                @edit-label="editLabel" @delete-node="deleteNode" />
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue';

const props = defineProps({
    node: Object
});

const emit = defineEmits(['add-child', 'edit-label', 'delete-node']);

const newLabel = ref(props.node.label);
const isEditing = ref(false);

function addChild() {
    emit('add-child', props.node.id);
}

function toggleEdit() {
    isEditing.value = !isEditing.value;
    newLabel.value = props.node.label; // Reset the newLabel when toggling edit mode
}

function saveLabel() {
    if (newLabel.value.trim() !== '') {
        emit('edit-label', { nodeId: props.node.id, newLabel: newLabel.value });
        isEditing.value = false;
    }
}

function deleteNode() {
    emit('delete-node', props.node.id);
}

// Expose functions to parent for editing and deleting
defineExpose({
    editLabel: saveLabel,
    deleteNode
});
</script>

<style lang="scss" scoped>
//子节点
.children {
    position: relative;
    height: fit-content;


    .children-mind-node {

        // 每个节点前面加横接线
        &::before {
            content: "";
            display: block;
            position: absolute;
            width: 20px; // 调整宽度以匹配子节点的左边距
            height: 1px; // 厚度设为1px以匹配横接线的宽度
            border-top: 1px solid #adb0b8;
            left: 0;
            top: 50%;
            transform: translateY(-50%); // 垂直居中
        }

        .child-insert {
            position: relative;
            display: flex;
            align-items: center;
            z-index: 1;
            min-height: 72px;
            margin-left: 36px;
            margin-top: 25px;

            .node-content {
                position: relative;
                padding: 12px 16px;
                background-color: var(--base-bg);
                box-shadow: var(--box-line-shadow);
                white-space: nowrap;
                max-height: 70px;
                min-width: 236px;
                max-width: 396px;
                border-radius: 8px;
                cursor: move;

                //内容标题
                .title {
                    font-size: var(--base-font-size);
                }
            }

            // 每个节点前面加横接线
            &::after {
                content: "";
                display: block;
                position: absolute;
                width: 15px; // 调整宽度以匹配你的设计需求
                height: 1px; // 厚度设为1px以匹配横接线的宽度
                border-top: 1px solid #adb0b8;
                left: -15px; // 调整位置以连接到竖接线
                top: 50%;
                transform: translateY(-50%); // 垂直居中
            }
        }
    }

    // 多节点加竖接线
    &::before {
        content: "";
        display: block;
        position: absolute;
        width: 1px; // 厚度设为1px
        height: 50%; // 高度设为100%以连接所有子节点
        border-left: 1px solid #adb0b8;
        left: 20px; // 调整位置以匹配横接线的右边距
        top: 35%;
    }
}
</style>